/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package control;

import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;

import java.util.List;
import pojo.Thread;
import pojo.User;
import pojo.Category;
import pojo.Pointdispatchhistory;
import service.IThreadService;
import service.IUserService;
import service.IPointService;
import service.ICategoryService;

/**
 *
 * @author administrator
 */
public class ThreadActionAdmin extends ActionSupport {
    private IThreadService<Thread> threadService;
    private IUserService<User> userService;
    private IPointService<Pointdispatchhistory> pointService;
    private ICategoryService<Category> categoryService;
    private int threadID;
    private Thread thread;
    private int categoryID;
    private String categoryName;
    private int type;
    private Boolean top;
    private Boolean highlight;
    private Boolean digest;
    private Boolean reply;

    public ICategoryService<Category> getCategoryService() {
        return categoryService;
    }

    public void setCategoryService(ICategoryService<Category> categoryService) {
        this.categoryService = categoryService;
    }


    public Boolean getDigest() {
        return digest;
    }

    public void setDigest(Boolean digest) {
        this.digest = digest;
    }

    public Boolean getHighlight() {
        return highlight;
    }

    public void setHighlight(Boolean highlight) {
        this.highlight = highlight;
    }

    public Boolean getReply() {
        return reply;
    }

    public void setReply(Boolean reply) {
        this.reply = reply;
    }

    public Boolean getTop() {
        return top;
    }

    public void setTop(Boolean top) {
        this.top = top;
    }

    public IPointService<Pointdispatchhistory> getPointService() {
        return pointService;
    }

    public void setPointService(IPointService<Pointdispatchhistory> pointService) {
        this.pointService = pointService;
    }


    public IUserService<User> getUserService() {
        return userService;
    }

    public void setUserService(IUserService<User> userService) {
        this.userService = userService;
    }


    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    



    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }



    public int getCategoryID() {
        return categoryID;
    }

    public void setCategoryID(int categoryID) {
        this.categoryID = categoryID;
    }



    public Thread getThread() {
        return thread;
    }

    public void setThread(Thread thread) {
        this.thread = thread;
    }

    public int getThreadID() {
        return threadID;
    }

    public void setThreadID(int threadID) {
        this.threadID = threadID;
    }

    public IThreadService<Thread> getThreadService() {
        return threadService;
    }

    public void setThreadService(IThreadService<Thread> threadService) {
        this.threadService = threadService;
    }

    public String delete()
    {
        this.thread = this.threadService.GetThread(this.threadID);
//        this.categoryID = this.thread.getCategory().getCategoryId();
//        this.categoryName = this.thread.getCategory().getCategoryName();
        if (this.thread != null) {
            this.changePoint(-5, "thread was deleted by moderator/admin");
            this.categoryService.decreaseArticle(this.thread.getCategory());
            this.threadService.deleteThread(this.thread);
        }
        this.type = 1;
        return "main";
    }

    public String setting()
    {
        this.thread = this.threadService.GetThread(this.threadID);
        return "setting";
    }

    public String submitSetting()
    {
       this.thread = this.threadService.GetThread(this.threadID);
       if(!(this.thread.getTop() == this.top))
       {
           if(this.top == true)
               this.changePoint(50, "thread was topped");
           else
               this.changePoint(-50, "thread was untopped");
           this.thread.setTop(this.top);
       }
       if(!(this.thread.getHighlight() == this.highlight))
       {
           if(this.highlight == true)
               this.changePoint(20, "thread was highlight");
           else
               this.changePoint(-20, "thread was recovered from hightlight");
           this.thread.setHighlight(this.highlight);
       }
       if(!(this.thread.getDigest() == this.digest))
       {
           if(this.digest == true)
               this.changePoint(100, "thread was added to digest");
           else
               this.changePoint(-100, "thread was deleted from digest");
           this.thread.setDigest(this.digest);
       }
       this.thread.setReply(this.reply);
       this.threadService.updateThread(thread);
       this.type = 2;
       return "main";

    }

    public void changePoint(int value,String reason)
    {
        User user = this.thread.getUser();
        this.userService.increasePoint(user, value);
        Pointdispatchhistory point = new Pointdispatchhistory();
        point.setPointDispatchAmount(value);
        point.setPointDispatchDate(new Date());
        point.setPointDispatchReason(reason);
        point.setUser(user);
        this.pointService.createNewHistory(point);
    }

}
